package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.TPatientVo;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 患者Controller
 *
 * @author 杨一迪
 * @date 2024-12-28
 */
@Controller
@RequestMapping("/system/patient")
public class TPatientController extends BaseController {
    private String prefix = "system/patient" ;
    @Resource
    private ISysDeptService deptService;
    @Resource
    private ISysPostService postService;
    @Resource
    private ITWardService wardService;
    @Resource
    private ITBedService bedService;

    @Resource
    private ITDoctorService doctorService;

    @Autowired
    private ITPatientService tPatientService;

    @Autowired
    private ITNurseService nurseService;

    @RequiresPermissions("system:patient:view")
    @GetMapping()
    public String patient() {
        return prefix + "/patient" ;
    }

    /**
     * 查询患者列表
     */
    @RequiresPermissions("system:patient:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TPatient tPatient) {
        startPage();
        List<TPatient> list = tPatientService.selectTPatientList(tPatient);
        List<TPatientVo> patientVos = new ArrayList<>();
        list.forEach(patient -> {
            TPatientVo tPatientVo = new TPatientVo();
            BeanUtils.copyProperties(patient,tPatientVo);
            TBed tBed = bedService.selectTBedById(patient.getBedId());
            TWard tWard = wardService.selectTWardById(patient.getWardId());
            TNurse tNurse = nurseService.selectTNurseById(patient.getResponsibleNurseId());
            TDoctor tDoctor = doctorService.selectTDoctorById(patient.getAttendingDoctorId());
            SysDept sysDept = deptService.selectDeptById(patient.getDepartmentId());
            tPatientVo.setBedNumber(tBed.getBedNumber());
            tPatientVo.setDeptName(sysDept.getDeptName());
            tPatientVo.setWardNumber(tWard.getWardNumber());
            tPatientVo.setNurseName(tNurse.getName());
            tPatientVo.setDoctorName(tDoctor.getName());
            patientVos.add(tPatientVo);
        });
        return getDataTable(patientVos);
    }

    /**
     * 导出患者列表
     */
    @RequiresPermissions("system:patient:export")
    @Log(title = "患者", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TPatient tPatient) {
        List<TPatient> list = tPatientService.selectTPatientList(tPatient);
        ExcelUtil<TPatient> util = new ExcelUtil<TPatient>(TPatient.class);
        return util.exportExcel(list, "患者数据");
    }

    /**
     * 新增患者
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<SysDept> sysDepts = deptService.selectDeptList(new SysDept());
        List<SysPost> sysPosts = postService.selectPostList(new SysPost());
        List<TWard> tWards = wardService.selectTWardList(new TWard());

        TBed tBed = new TBed();
        tBed.setIsAvailable(0);
        List<TBed> tBeds = bedService.selectTBedList(tBed);
        List<TDoctor> tDoctors = doctorService.selectTDoctorList(new TDoctor());
        List<TNurse> tNurses = nurseService.selectTNurseList(new TNurse());
        mmap.put("depts",sysDepts);
        mmap.put("sysPosts",sysPosts);
        mmap.put("tWards",tWards);
        mmap.put("tBeds",tBeds);
        mmap.put("tDoctors",tDoctors);
        mmap.put("tNurses",tNurses);
        return prefix + "/add" ;
    }

    /**
     * 新增保存患者
     */
    @RequiresPermissions("system:patient:add")
    @Log(title = "患者", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TPatient tPatient) {
        return toAjax(tPatientService.insertTPatient(tPatient));
    }

    /**
     * 修改患者
     */
    @RequiresPermissions("system:patient:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TPatient tPatient = tPatientService.selectTPatientById(id);
        mmap.put("tPatient", tPatient);
        List<SysDept> sysDepts = deptService.selectDeptList(new SysDept());
        List<SysPost> sysPosts = postService.selectPostList(new SysPost());
        List<TWard> tWards = wardService.selectTWardList(new TWard());
        List<TBed> tBeds = bedService.selectTBedList(new TBed());
        List<TDoctor> tDoctors = doctorService.selectTDoctorList(new TDoctor());
        List<TNurse> tNurses = nurseService.selectTNurseList(new TNurse());
        mmap.put("depts",sysDepts);
        mmap.put("sysPosts",sysPosts);
        mmap.put("tWards",tWards);
        mmap.put("tBeds",tBeds);
        mmap.put("tDoctors",tDoctors);
        mmap.put("tNurses",tNurses);
        return prefix + "/edit" ;
    }

    /**
     * 修改保存患者
     */
    @RequiresPermissions("system:patient:edit")
    @Log(title = "患者", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TPatient tPatient) {
        return toAjax(tPatientService.updateTPatient(tPatient));
    }

    /**
     * 删除患者
     */
    @RequiresPermissions("system:patient:remove")
    @Log(title = "患者", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tPatientService.softDeleteTPatientByIds(ids));
    }
}
